# 1,获取所有的sku信息
from django.conf import settings
from fdfs_client.client import Fdfs_client
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet

from goods.models import SKU, SKUImage
from meiduo_admin.my_paginate import MyPageNumberPagination
from meiduo_admin.serializers import sku_image_Serializers
from utils.fdfs.MyFileStorage import MyStorage


class SKUImageView(ModelViewSet):
    pagination_class = MyPageNumberPagination
    serializer_class = sku_image_Serializers.SKUImageSerializer
    queryset = SKUImage.objects.all()

    def sku_simple(self, request):
        # 1,查询所有的sku数据
        queryset = SKU.objects.all()

        # 2,获取序列化器
        serializer = sku_image_Serializers.SKUSimpleSerializer(instance=queryset, many=True)

        # 3,返回响应
        return Response(serializer.data)

    # 2,重写create,上传图片
    def create(self, request, *args, **kwargs):
        # 1,获取图片,sku_id参数
        sku_id = request.data.get("sku")
        image = request.data.get("image")

        # 2,获取序列化器校验数据
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)

        # 3,入库(上传图片)
        # 3,1创建fastdfs对象
        client = Fdfs_client(settings.FDFS_CONFIG)
        result = client.upload_by_buffer(image.read())

        # 3,2判断是否上传成功
        if result["Status"] != "Upload successed.":
            return Response({"errmsg": "上传失败"}, status=400)

        # 3,3取出图片
        image_url = result["Remote file_id"]

        # 3,4入库
        SKUImage.objects.create(sku_id=sku_id, image=image_url)

        # 4,返回响应
        return Response(status=201)

    def save(self, name, content, max_length=None):
        """保存文件的时候调用"""
        #1,创建对象
        client = Fdfs_client(settings.FDFS_CONFIG)

        #2,上传
        result = client.upload_by_buffer(content.read())

        #3判断是否上传成功
        if result["Status"] != "Upload successed.":
            return None

        image_url = result["Remote file_id"]

        #4,返回图片
        return image_url

    #封装图片上传
    def upload_image(self, request):
        # 1,获取参数
        image = request.FILES.get("image")

        # 2,上传图片,判断是否上传成功
        image_url = MyStorage().save(None, image)
        if not image_url:
            return Response({"errmsg": "上传失败"}, status=400)

        # 3,返回响应
        return Response({
            "img_url": settings.BASE_URL + image_url
        })

    #3,重写update方法,修改图片
    def update(self, request, *args, **kwargs):
        #1,获取图片对象(id), sku, 图片地址
        image_obj = self.get_object() # 根据pk,获取了图片对象
        image = request.data.get("image")
        sku_id = request.data.get("sku")

        #2,获取序列化器校验数据
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)

        #3,入库(上传图片)
        #3,1上传图片
        image_url = MyStorage().save(None,image)
        if not image_url:
            return Response({"errmsg":"上传失败"},status=400)

        #3,4入库
        image_obj.image = image_url
        image_obj.sku_id = sku_id
        image_obj.save()

        #4,返回响应
        return Response(status=201)